package com.xxlie.auth.security.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.xxlie.auth.domain.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;

/**
 * UserContext
 *
 * @author xxlie.com
 * @since 2017/7/30
 */
public class UserContext {

    private final String username;
    private final Collection<GrantedAuthority> authorities;
    private Map<String, Object> cacheMap;
    private User user;
    private Map<String, Object> extraParams;

    private UserContext(String username, Collection<GrantedAuthority> authorities) {
        this.username = username;
        this.authorities = authorities;
    }

    public UserContext(User user, Collection<GrantedAuthority> authorities) {
        this.authorities = authorities;
        this.user = user;
        this.username = user.getUsername();
    }

    public static UserContext create(String username, Collection<GrantedAuthority> authorities) {
        if (StringUtils.isBlank(username)) throw new IllegalArgumentException("Username is blank: " + username);
        return new UserContext(username, authorities);
    }

    public String getUsername() {
        return username;
    }

    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public Map<String, Object> getCacheMap() {
        if (cacheMap == null) {
            cacheMap = new HashMap<>();
        }
        return cacheMap;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

	public Map<String, Object> getExtraParams() {
		return extraParams;
	}

	public void setExtraParams(Map<String, Object> extraParams) {
		this.extraParams = extraParams;
	}
}
